// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import GameEvent from "../../Commons/GameEvent";
import SaveManager from "../../Managers/SaveManager";
import PetManager, { PetEvent } from "../../Models/Pets/PetManager";
import BaseUI from "../BaseUI";
import PetItem from "../UIItems/PetItem";

const { ccclass, property } = cc._decorator;

@ccclass
export default class PetUI extends BaseUI {

    @property(cc.Prefab)
    petItemPrefab: cc.Prefab = null;
    @property(cc.ScrollView)
    scrollview: cc.ScrollView = null;
    @property([PetItem])
    petItemArr: PetItem[] = [];
    @property(cc.Label)
    attributeTxt0: cc.Label = null;
    @property(cc.Label)
    attributeTxt1: cc.Label = null;
    @property(cc.Button)
    takeOffBtn: cc.Button = null;
    @property(cc.Button)
    fightBtn: cc.Button = null;
    @property(sp.Skeleton)
    petSkeleton: sp.Skeleton = null;

    currentItem: PetItem = null;

    protected onLoad(): void {
        var arr = PetManager.single.getPetTemplateArr();
        for (let i = 0; i < arr.length; i++) {
            var itemNode = cc.instantiate(this.petItemPrefab);
            itemNode.active = false;
            this.scrollview.content.addChild(itemNode);
        }
    }

    protected onEnable(): void {
        this.drawPetWear();
        this.drawPetBag();
        this.drawPetInfo();
        GameEvent.eventNode.on(PetEvent.PETFIGHT_EVENT, this.onUpdatePet, this);
        GameEvent.eventNode.on(PetEvent.PETTAKEOFF_EVENT, this.onUpdatePet, this);
        GameEvent.eventNode.on(PetEvent.ADDNEWPET_EVENT, this.onUpdatePet, this);
    }

    protected onDisable(): void {
        GameEvent.eventNode.off(PetEvent.PETFIGHT_EVENT, this.onUpdatePet, this);
        GameEvent.eventNode.off(PetEvent.PETTAKEOFF_EVENT, this.onUpdatePet, this);
        GameEvent.eventNode.off(PetEvent.ADDNEWPET_EVENT, this.onUpdatePet, this);
    }

    onUpdatePet() {
        this.drawPetWear();
        this.drawPetBag();
    }
    drawPetWear() {
        var arr = SaveManager.single.userdata.petWearList;
        for (let i = 0; i < this.petItemArr.length; i++) {
            const element = this.petItemArr[i];
            var ey = PetManager.single.getPetEntity(arr[i]);
            element.drawItem(ey, i, arr[i]);
        }
    }

    drawPetBag() {
        var arr = SaveManager.single.userdata.petBagList;
        var nodeArr = this.scrollview.content.children;
        for (const iterator of nodeArr) {
            iterator.active = false;
        }

        for (let i = 0; i < arr.length; i++) {
            const element = arr[i];
            var itemNode = nodeArr[i];

            itemNode.active = true;
            var script = itemNode.getComponent(PetItem);
            script.itemClickFun = this.petItemClick;
            script.itemCaller = this;
            script.drawItem(element, i, 0);
        }
    }

    petItemClick(item: PetItem) {
        if (this.currentItem != null) {
            this.currentItem.selectNode.active = false;
        }
        this.currentItem = item;
        this.currentItem.selectNode.active = true;
        this.drawPetInfo();
    }

    drawPetInfo() {
        this.takeOffBtn.node.active = this.fightBtn.node.active = false;
        this.attributeTxt0.string = "";
        this.attributeTxt1.string = "";
        if (this.currentItem != null) {
            if (this.currentItem.petEy != null) {
                this.takeOffBtn.node.active = this.currentItem.petEy.isFight
                this.fightBtn.node.active = !this.currentItem.petEy.isFight;
                this.attributeTxt0.string = this.currentItem.petEy.attributeDes0;
                this.attributeTxt1.string = this.currentItem.petEy.attributeDes1;
                var that = this;
                cc.resources.load("pets/" + this.currentItem.petEy.petTp.petSkelentonName, sp.SkeletonData, (error: Error, assets: sp.SkeletonData) => {
                    that.petSkeleton.skeletonData = assets;
                    that.petSkeleton.setAnimation(0, "idle2", true);
                });
            }
        }
    }
    wearClick() {
        if (this.currentItem == null) {
            return;
        }
        if (this.currentItem.petEy != null) {
            PetManager.single.requestPetFight(this.currentItem.petEy.id);
        }

    }
    takeoffClick() {
        if (this.currentItem == null) {
            return;
        }
        if (this.currentItem.petEy != null) {
            PetManager.single.requestPetTakeoff(this.currentItem.petEy.id);
        }
    }



}
